﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared



Partial Class Reports_report_stp_main_system2
    Inherits System.Web.UI.Page
    Private report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        CrystalReportViewer1.ReportSource = report
        CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX
    End Sub
    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        If Not (CrystalReportViewer1 Is Nothing) Then
            CrystalReportViewer1.Dispose()
        End If
    End Sub

    Protected Sub CrystalReportViewer1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Unload
        If Not (report Is Nothing) Then
            report.Close()
            report.Dispose()
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dw As stp_main_systemTableAdapters.USP_List_stp_main_systemTableAdapter
        Dim dt As DataTable
        Dim strSystemType As String
        Dim strSystemStatus As String
        Dim iType As Integer
        Dim iStatus As Integer
        Try
            'get Querystring
            iType = Request.QueryString("main_system_type")
            iStatus = Request.QueryString("main_system_status")
            strSystemType = Request.QueryString("str_main_system_type")
            strSystemStatus = Request.QueryString("str_main_system_status")
            'check status , 0: ทั้งหมด  ,1: ใช้ , 2: ไม่ใช้
            strSystemStatus = IIf(iStatus = 0, "ทั้งหมด", strSystemStatus)
            strSystemType = IIf(iType = 0, "ทั้งหมด", strSystemType)

            dw = New stp_main_systemTableAdapters.USP_List_stp_main_systemTableAdapter
            'Retrieve data from DB & collect to datatable
            dt = dw.GetData(iStatus, iType)

            'check 
            If (dt.Rows.Count > 0) Then
                'Create report engine
                'Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                'map report parth
                report.Load(Server.MapPath("report_stp_main_system.rpt"))
                'set data table to report
                report.SetDataSource(dt)
                'set input paratmenter
                report.SetParameterValue("SystemType", strSystemType)
                report.SetParameterValue("StatusType", strSystemStatus)

                'Show report
                ' CrystalReportViewer1.ReportSource = report
            Else
                ' hide invalid message
                CrystalReportViewer1.Visible = False

                GenericHelper.ShowAlert(Me, "ไม่พบข้อมูล")
                Dim strscript As String = "<script language=javascript>window.top.close();</script>"
                If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                    Page.RegisterStartupScript("clientScript", strscript)
                End If
                ' TODO:
                ' Redirect to pervious page
            End If
        Catch ex As Exception
            ' hide invalid message
            CrystalReportViewer1.Visible = False

            GenericHelper.ShowAlert(Me, ex.Message)
            Dim strscript As String = "<script language=javascript>window.top.close();</script>"
            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strscript)
            End If
        Finally
            If Not (dt Is Nothing) Then
                dt.Dispose()
            End If
            If Not (dw Is Nothing) Then
                dw.Dispose()
            End If
        End Try
    End Sub
End Class
